package com.aipartner.mapper;

import com.aipartner.entity.UserWorkLike;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 用户作品点赞Mapper
 * 
 * @author AI Partner Team
 * @since 2024-01-20
 */
@Mapper
public interface UserWorkLikeMapper extends BaseMapper<UserWorkLike> {
    
    /**
     * 获取作品点赞数
     */
    @Select("SELECT COUNT(*) FROM user_work_likes WHERE work_id = #{workId}")
    int getLikeCount(@Param("workId") Long workId);
    
    /**
     * 检查用户是否已点赞
     */
    @Select("SELECT COUNT(*) > 0 FROM user_work_likes WHERE work_id = #{workId} AND user_id = #{userId}")
    boolean isLikedByUser(@Param("workId") Long workId, @Param("userId") Long userId);
    
    /**
     * 删除用户点赞记录
     */
    @org.apache.ibatis.annotations.Delete("DELETE FROM user_work_likes WHERE work_id = #{workId} AND user_id = #{userId}")
    int deleteLike(@Param("workId") Long workId, @Param("userId") Long userId);
    
    /**
     * 更新作品点赞数
     */
    @Update("UPDATE user_works SET like_count = (SELECT COUNT(*) FROM user_work_likes WHERE work_id = #{workId}) WHERE id = #{workId}")
    void updateWorkLikeCount(@Param("workId") Long workId);
}
